package com.yh.jandan.ext;

import com.alibaba.druid.filter.stat.StatFilter;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.druid.DruidStatViewHandler;
import com.jfinal.render.ViewType;
import com.yh.jandan.controller.ConfigController;
import com.yh.jandan.controller.JandanController;
import com.yh.jandan.controller.MeiziController;
import com.yh.jandan.ext.thread.MeiziCheckThread;
import com.yh.jandan.ext.thread.MeiziSaveThread;
import com.yh.jandan.model.Meizi;
import com.yh.jandan.model._MappingKit;

public class WebApp extends JFinalConfig {
	
	public final static String _CONFIG_ = "a_little_config.txt";
	
	public void configConstant ( Constants me ) {
		Prop p = PropKit.use ( _CONFIG_ );
		me.setDevMode ( p.getBoolean ( "devMode" , false ) );
		me.setViewType ( ViewType.JSP );
	}
	
	public void configRoute ( Routes me ) {
		me.add ( "/config" , ConfigController.class );
		me.add ( "/meizi" , MeiziController.class );
		me.add ( "/" , JandanController.class );
	}
	
	public void configPlugin ( Plugins me ) {
		Prop p = PropKit.use ( _CONFIG_ );
		// C3p0Plugin c3p0 = new C3p0Plugin( p.get( "jdbcUrl" ).trim() , p.get(
		// "user" ) , p.get( "password" ) ,//
		// "org.sqlite.JDBC" , 30 , 1 , 1 , 60 , 5 );
		// // c3p0.start();
		// me.add( c3p0 );
		DruidPlugin dp = new DruidPlugin ( p.get ( "jdbcUrl" ).trim ( ) , p.get ( "user" ) , p.get ( "password" ) );
		dp.addFilter ( new StatFilter ( ) );
		dp.setMinIdle ( 5 )//
				.setTimeBetweenEvictionRunsMillis ( 6 * 1000 )//
				.setValidationQuery ( "select 1" )//
				.setTestWhileIdle ( true )//
				.setTestOnBorrow ( true )//
				.setTestOnReturn ( true ); //
		// .setMaxPoolPreparedStatementPerConnectionSize( 0 );
		// //druid和sqlite配合不能用该参数.
		
		// WallFilter wall = new WallFilter();
		// wall.setDbType( "mysql" );
		// dp.addFilter( wall );
		me.add ( dp );
		// /
		ActiveRecordPlugin arp = new ActiveRecordPlugin ( dp );
		// arp.setDialect ( new Sqlite3Dialect ( ) ) ;
		arp.setDialect ( new MysqlDialect ( ) );
		// arp.setDevMode( p.getBoolean( "devMode" , false ) );
		// arp.setShowSql( p.getBoolean( "devMode" , false ) );
		me.add ( arp );
		//
		_MappingKit.mapping ( arp );
	}
	
	public void configInterceptor ( Interceptors me ) {}
	
	public void configHandler ( Handlers me ) {
		me.add ( new ContextPathHandler ( "ctx" ) );
		me.add ( new DruidStatViewHandler ( "/druid" ) );
	}
	
	public void afterJFinalStart ( ) {
		super.afterJFinalStart ( );
		Prop p = PropKit.use ( _CONFIG_ );
		try {
			Init.initData ( );
			//
			Init.initKey ( p.get ( "webkey" ) ); // 注册数量
			new Thread ( new Runnable ( ) {
				public void run ( ) {
					Prop p = PropKit.use ( _CONFIG_ );
					// LocalApp.autocatch ( ) ;
					// WatchThread.start ( ) ;
					Meizi.init_ok_url_list ( );
				        if ( p.getBoolean ( "SaveMode" , false ) ) MeiziSaveThread.start ( );
                         		if ( p.getBoolean ( "CheckMode" , false ) ) MeiziCheckThread.start ( );
					// MeiziUpdateThread.start ( ) ;
				}
			} ).start ( );
			
		} catch ( Exception e ) {
			e.printStackTrace ( );
		}
	}
	
	@Override
	public void beforeJFinalStop ( ) {
		super.beforeJFinalStop ( );
		// LocalApp.stop ( ) ;
		// WatchThread.stop ( ) ;
		Meizi.stop_init_ok_url_list ( );
		// MeiziUpdateThread.stop ( ) ;
		MeiziSaveThread.stop ( );
		MeiziCheckThread.stop ( );
	}
	
}
